package utils;

import dtos.*;
import model.*;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Session Bean implementation class Conv
 */
@Stateless
@Local
public class Conv {

    @PersistenceContext(unitName = "rmtPersistenceUnit")
    EntityManager em;


    /**
     * Default constructor.
     */
    public Conv() {
    }

    /**
     * Method used to convert from Response to ResponseDTO
     *
     * @param response type Response
     * @return of type ResponseDTO
     */
    public ResponseDTO fromEntityResponse(Response response) {
        ResponseDTO rd = new ResponseDTO();
        rd.setDate(response.getDate());
        rd.setDescription(response.getDescription());
        rd.setId(response.getId());
        rd.setIdUser(response.getUser().getId());
        return rd;
    }

    /**
     * Method used to convert from Doamin to DomainDTO
     *
     * @param domain type Domain
     * @return of type DomainDTO
     */
    public DomainDTO fromEntityDomain(Domain domain) {
        DomainDTO dd = new DomainDTO();

        dd.setId(domain.getId());
        dd.setName(domain.getName());
        if (domain.getUsers() != null && !domain.getUsers().isEmpty())
            for (User u : domain.getUsers()) {
                dd.addUser(u.getId());
            }
        if (domain.getTechresources() != null && !domain.getTechresources().isEmpty())
            for (Techresource t : domain.getTechresources()) {
                dd.addTechresource(t.getId());
            }
        return dd;


    }

    /**
     * Method used to convert from Usertype to UsertypeDTO
     *
     * @param type type Usertype
     * @return of type UsertypeDTO
     */
    public UsertypeDTO fromEntityUsertype(Usertype type) {
        UsertypeDTO ud = new UsertypeDTO();
        ud.setId(type.getId());
        ud.setName(type.getName());
        return ud;
    }

    /**
     * Method used to convert from User to UserDTO
     *
     * @param u type User
     * @return of type UserDTO
     */
    public UserDTO fromEntityUser(User u) {
        UserDTO ud = new UserDTO();
        ud.setId(u.getId());
        if (u.getDomain() != null)
            ud.setIdDomain(u.getDomain().getId());
        ud.setIsActive(u.getIsactive());
        ud.setEmail(u.getEmail());
        ud.setName(u.getName());
        ud.setPassword(u.getPassword());
        ud.setUsername(u.getUsername());
        if (u.getUsertype() != null)
            ud.setIdUsertype(u.getUsertype().getId());
        ud.setStatus(u.getStatus());
        ud.setIp(u.getIp());
        if (u.getUserlogs() != null && !u.getUserlogs().isEmpty())
            for (Userlog ul : u.getUserlogs())
                ud.addUserlogs(ul.getId());
        if (u.getUser() != null && u.getUser().getId() != 0) {
            ud.setIdUser(u.getUser().getId());
        }
        if (u.getLeaves() != null) {
            for (Leave l : u.getLeaves()) {
                ud.getLeaves().add(toDtoLeave(l));
            }
        }

        return ud;
    }

    /**
     * Method used to convert from Userlog to UserlogDTO
     *
     * @param u type Userlog
     * @return of type UserlogDTO
     */
    public UserlogDTO fromEntityUserlog(Userlog u) {
        UserlogDTO userlog = new UserlogDTO();
        userlog.setId(u.getId());
        userlog.setEndtime(u.getEndtime());
        userlog.setStarttime(u.getStarttime());
        if (u.getUser() != null)
            userlog.setIdUser(u.getUser().getId());
        return userlog;
    }

    /**
     * Method used to convert from Techresource to TechresourceDTO
     *
     * @param t type Techresource
     * @return of type TechresourceDTO
     */
    public TechresourceDTO fromEntityTechresource(Techresource t) {
        TechresourceDTO td = new TechresourceDTO();
        td.setId(t.getId());
        if (t.getDomain() != null)
            td.setIdDomain(t.getDomain().getId());
        td.setIp(t.getIp());
        td.setIdnumber(t.getIdnumber());
        td.setName(t.getName());
        td.setStatus(t.getStatus());
        if (t.getTechtickets() != null && !t.getTechtickets().isEmpty())
            for (Techticket tt : t.getTechtickets()) {
                td.addTechticket(tt.getId());
            }
        if (t.getThreshresources() != null && !t.getThreshresources().isEmpty())
            for (Threshresource ts : t.getThreshresources()) {
                td.addThreshresource(ts.getId());
            }
        return td;
    }

    /**
     * Method used to convert from Resource to ResourceDTO
     *
     * @param r type Resource
     * @return of type ResourceDTO
     */
    public ResourceDTO fromEntityResource(Resource r) {
        ResourceDTO rd = new ResourceDTO();
        rd.setId(r.getId());
        rd.setName(r.getName());
        if (r.getRestickets() != null && !r.getRestickets().isEmpty())
            for (Resticket t : r.getRestickets()) {
                rd.addResticket(t.getId());
            }
        return rd;
    }

    /**
     * Method used to convert from Ticket to TicketDTO
     *
     * @param t type Ticket
     * @return of type TicketDTO
     */
    public TicketDTO fromEntityTicket(Ticket t) {
        TicketDTO td = new TicketDTO();
        td.setDate(t.getDate());
        td.setId(t.getId());
        td.setDescription(t.getDescription());
        td.setStatus(t.getStatus());
        if (t.getResponse() != null)
            td.setIdResponse(t.getResponse().getId());
        if (t.getUser() != null)
            td.setIdUser(t.getUser().getId());
        return td;
    }

    /**
     * Method used to convert from Techticket to TechticketDTO
     *
     * @param t type Techticket
     * @return of type TechticketDTO
     */
    public TechticketDTO fromEntityTechticket(Techticket t) {
        TechticketDTO td = new TechticketDTO();
        td.setId(t.getId());
        td.setDate(t.getDate());
        td.setDescription(t.getDescription());
        if (t.getResponse() != null)
            td.setIdResponse(t.getResponse().getId());
        td.setStatus(t.getStatus());

        if (t.getUser() != null)
            td.setIdUser(t.getUser().getId());

        if (t.getProblemtype() != null)
            td.setProblemtype(t.getProblemtype());

        if (t.getTechresource() != null)
            td.setIdTechresource(t.getTechresource().getId());
        return td;
    }

    /**
     * Method used to convert from Resticket to ResticketDTO
     *
     * @param t type Resticket
     * @return of type ResticketDTO
     */
    public ResticketDTO fromEntityResticket(Resticket t) {
        ResticketDTO rd = new ResticketDTO();
        rd.setId(t.getId());
        rd.setDescription(t.getDescription());
        rd.setDate(t.getDate());
        rd.setStatus(t.getStatus());
        if (t.getUser() != null)
            rd.setIdUser(t.getUser().getId());
        if (t.getMeasureunit() != null)
            rd.setItMeasureunit(t.getMeasureunit().getId());
        if (t.getResponse() != null)
            rd.setIdResponse(t.getResponse().getId());
        rd.setQuantity(t.getQuantity());
        if (t.getResource() != null)
            rd.setIdResource(t.getResource().getId());
        return rd;
    }

    /**
     * Method used to convert from Measureunit to MeasureunitDTO
     *
     * @param u type Measureunit
     * @return of type MeasureunitDTO
     */
    public MeasureunitDTO fromEntityMeasureunit(Measureunit u) {
        MeasureunitDTO md = new MeasureunitDTO();
        md.setId(u.getId());
        md.setName(u.getName());
        for (Resticket t : u.getRestickets()) {
            md.addResticket(t.getId());
        }
        return md;
    }

    /**
     * Method used to convert from Threshresource to ThreshresourceDTO
     *
     * @param t type Threshresource
     * @return of type ThreshresourceDTO
     */
    public ThreshresourceDTO fromEntityThreshresource(Threshresource t) {
        ThreshresourceDTO tr = new ThreshresourceDTO();
        tr.setName(t.getName());
        tr.setValue(t.getValue());
        if (t.getTechresources() != null && !t.getTechresources().isEmpty()) {
            for (Techresource ti : t.getTechresources()) {
                tr.addTechresource(ti.getId());
            }
        }
        return tr;
    }

    /**
     * Method used to convert from DomainDTO to Domain
     *
     * @param d type DomainDTO
     * @return of type Domain
     */
    public Domain fromEntityDomainDTO(DomainDTO d) {
        Domain domain = new Domain();
        domain.setName(d.getName());
        domain.setId(d.getId());
        if (d.getUsers() != null)
            for (Integer i : d.getUsers()) {
                domain.addUser(em.find(User.class, i));
            }
        if (d.getTechresources() != null && !d.getTechresources().isEmpty())
            for (Integer i : d.getTechresources()) {
                domain.addTechresource(em.find(Techresource.class, i));
            }
        return domain;
    }

    /**
     * Method used to convert from MeasureunitDTO to Measureunit
     *
     * @param m type MeasureunitDTO
     * @return of type Measureunit
     */
    public Measureunit fromEntityMeasureunitDTO(MeasureunitDTO m) {
        Measureunit measure = new Measureunit();
        measure.setName(m.getName());
        measure.setId(m.getId());
        if (m.getRestickets() != null && !m.getRestickets().isEmpty())
            for (Integer i : m.getRestickets()) {
                measure.addResticket(em.find(Resticket.class, i));
            }
        return measure;
    }

    /**
     * Method used to convert from ResourceDTO to Resource
     *
     * @param d type ResourceDTO
     * @return of type Resource
     */
    public Resource fromEntityResourceDTO(ResourceDTO d) {
        Resource res = new Resource();
        res.setId(d.getId());
        res.setName(d.getName());
        if (d.getRestickets() != null && !d.getRestickets().isEmpty())
            for (Integer i : d.getRestickets()) {
                res.addResticket(em.find(Resticket.class, i));
            }
        return res;
    }

    /**
     * Method used to convert from ResponseDTO to Response
     *
     * @param r type ResponseDTO
     * @return of type Response
     */
    public Response fromEntityResponseDTO(ResponseDTO r) {
        Response resp = new Response();
        resp.setId(r.getId());
        resp.setDate(r.getDate());
        resp.setUser(em.find(User.class, r.getIdUser()));
        resp.setDescription(r.getDescription());
        return resp;

    }

    /**
     * Method used to convert from ResticketDTO to Resticket
     *
     * @param r type ResticketDTO
     * @return of type Resticket
     */
    public Resticket fromEntityResticketDTO(ResticketDTO r) {
        Resticket res = new Resticket();
        res.setUser(em.find(User.class, r.getIdUser()));
        res.setId(r.getIdResticket());
        res.setStatus(r.getStatus());
        res.setDate(r.getDate());
        res.setDescription(r.getDescription());
        res.setQuantity(r.getQuantity());
        res.setResource(em.find(Resource.class, r.getIdResource()));
        res.setMeasureunit(em.find(Measureunit.class, r.getItMeasureunit()));
        res.setTicket(em.find(Ticket.class, r.getIdTicket()));
        return res;
    }

    /**
     * Method used to convert from TechresourceDTO to Techresource
     *
     * @param t type TechresourceDTO
     * @return of type Techresource
     */
    public Techresource fromEntityTechresourceDTO(TechresourceDTO t) {
        Techresource tech = new Techresource();
        tech.setId(t.getId());
        tech.setName(t.getName());
        tech.setIp(t.getIp());
        tech.setStatus(t.getStatus());
        tech.setIdnumber(t.getIdnumber());
        if (tech.getDomain() != null)
            tech.setDomain(em.find(Domain.class, t.getIdDomain()));
        if (t.getTechtickets() != null && !t.getTechtickets().isEmpty())
            for (Integer i : t.getTechtickets()) {
                tech.addTechticket(em.find(Techticket.class, i));
            }
        if (t.getThreshresources() != null && !t.getThreshresources().isEmpty())
            for (Integer i : t.getThreshresources()) {
                tech.addThreshresource(em.find(Threshresource.class, i));
            }

        return tech;
    }

    /**
     * Method used to convert from TechticketDTO to Techticket
     *
     * @param r type TechticketDTO
     * @return of type Techticket
     */
    public Techticket fromEntityTechticketDTO(TechticketDTO r) {
        Techticket tick = new Techticket();
        tick.setId(r.getIdTicket());
        tick.setUser(em.find(User.class, r.getIdUser()));
        tick.setProblemtype(r.getProblemtype());
        tick.setStatus(r.getStatus());
        tick.setDate(r.getDate());
        tick.setDescription(r.getDescription());
        tick.setTicket(em.find(Ticket.class, r.getIdTechticket()));

        tick.setTechresource(em.find(Techresource.class, r.getIdTechresource()));
        return tick;
    }

    /**
     * Method used to convert from ThreshresourceDTO to Threshresource
     *
     * @param t type ThreshresourceDTO
     * @return of type Threshresource
     */
    public Threshresource fromEntityThreshresourceDTO(ThreshresourceDTO t) {
        Threshresource tr = new Threshresource();
        tr.setId(t.getId());
        if (t.getTechresources() != null && !t.getTechresources().isEmpty())
            for (Integer i : t.getTechresources()) {
                tr.addTechresource(em.find(Techresource.class, i));
            }
        tr.setName(t.getName());
        tr.setValue(t.getValue());
        return tr;
    }

    /**
     * Method used to convert from UserDTO to User
     *
     * @param u type UserDTO
     * @return of type User
     */
    public User fromEntityUserDTO(UserDTO u) {
        User user = new User();
        user.setId(u.getId());
        user.setName(u.getName());
        user.setUsername(u.getUsername());
        user.setPassword(u.getPassword());
        user.setEmail(u.getEmail());
        user.setIsactive(u.getIsActive());
        user.setUser(em.find(User.class, u.getIdUser()));
        user.setDomain(em.find(Domain.class, u.getIdDomain()));
        user.setUsertype(em.find(Usertype.class, u.getIdUsertype()));
        user.setStatus(u.getStatus());
        user.setIp(u.getIp());
        if (u.getUserlogs() != null)
            for (Integer i : u.getUserlogs()) {
                user.addUserlog(em.find(Userlog.class, i));
            }
        if (u.getLeaves() != null) {
            for (LeaveDTO l : u.getLeaves()) {
                user.getLeaves().add(toEntityLeave(l));
            }
        }
        return user;

    }

    /**
     * Method used to convert from TicketDTO to Ticket
     *
     * @param t type TicketDTO
     * @return of type Ticket
     */
    public Ticket fromEntityTicketDTO(TicketDTO t) {
        Ticket ticket = new Ticket();
        ticket.setId(t.getId());
        ticket.setStatus(t.getStatus());
        ticket.setDescription(t.getDescription());
        ticket.setDate(t.getDate());
        ticket.setResponse(em.find(Response.class, t.getIdResponse()));
        ticket.setResticket(em.find(Resticket.class, t.getIdResticket()));
        ticket.setTechticket(em.find(Techticket.class, t.getIdTechticket()));
        ticket.setUser(em.find(User.class, t.getIdUser()));
        return ticket;
    }

    /**
     * Method used to convert from UserlogDTO to Userlog
     *
     * @param u type UserlogDTO
     * @return of type Userlog
     */
    public Userlog fromEntityUserlogDTO(UserlogDTO u) {
        Userlog userlog = new Userlog();
        userlog.setId(u.getId());
        userlog.setDate(u.getDate());
        userlog.setEndtime(u.getEndtime());
        userlog.setStarttime(u.getStarttime());

        userlog.setUser(em.find(User.class, u.getIdUser()));
        return userlog;
    }

    /**
     * Method used to convert from UsertypeDTO to Usertype
     *
     * @param u type UsertypeDTO
     * @return of type Usertype
     */
    public Usertype fromEntityUsertypeDTO(UsertypeDTO u) {
        Usertype ut = new Usertype();
        ut.setId(u.getId());
        ut.setName(u.getName());
        if (u.getUsers() != null && !u.getUsers().isEmpty())
            for (Integer i : u.getUsers()) {
                ut.addUser(em.find(User.class, i));
            }

        return ut;
    }

    public LeaveType toEntityLeaveType(LeaveTypeDTO leaveTypeDTO) {
        LeaveType leaveType = new LeaveType();
        leaveType.setName(leaveTypeDTO.getName());
        leaveType.setId(leaveTypeDTO.getId());
        return leaveType;
    }



    public LeaveTypeDTO toDtoLeaveType(LeaveType leaveType) {
        LeaveTypeDTO leaveTypeDTO = new LeaveTypeDTO();
        leaveTypeDTO.setId(leaveType.getId());
        leaveTypeDTO.setName(leaveType.getName());
        return leaveTypeDTO;
    }
    public LeaveStatus toEntityLeaveStatus(LeaveStatusDTO leaveStatusDTO) {
        LeaveStatus leaveStatus = new LeaveStatus();
        leaveStatus.setName(leaveStatusDTO.getName());
        leaveStatus.setId(leaveStatusDTO.getId());
        return leaveStatus;
    }

    public LeaveStatusDTO toDtoLeaveStatus(LeaveStatus leaveStatus) {
        LeaveStatusDTO leaveStatusDTO = new LeaveStatusDTO();
        leaveStatusDTO.setId(leaveStatus.getId());
        leaveStatusDTO.setName(leaveStatus.getName());
        return leaveStatusDTO;
    }

    public Leave toEntityLeave(LeaveDTO leaveDTO) {
        Leave leave = new Leave();
        leave.setId(leaveDTO.getId());
        leave.setFrom(leaveDTO.getFrom());
        leave.setUntil(leaveDTO.getUntil());
        leave.setReason(leaveDTO.getReason());
        leave.setUser(em.find(User.class, leaveDTO.getUser()));
        leave.setStatus(em.find(LeaveStatus.class, leaveDTO.getStatus()));
        leave.setType(em.find(LeaveType.class, leaveDTO.getType()));
        return leave;
    }

    public LeaveDTO toDtoLeave(Leave leave) {
        LeaveDTO leaveDTO = new LeaveDTO();
        leaveDTO.setId(leave.getId());
        leaveDTO.setFrom(leave.getFrom());
        leaveDTO.setUntil(leave.getUntil());
        leaveDTO.setReason(leave.getReason());
        leaveDTO.setUser(leave.getUser().getId());
        leaveDTO.setStatus(leave.getStatus().getId());
        leaveDTO.setType(leave.getType().getId());
        return leaveDTO;
    }
}

